package scheduler.classes;

import java.lang.reflect.Method;
import java.util.Hashtable;

import property.Property;
import utility.CapitalChar;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import event.Event;

public class Reminder implements Scheduler
{
	DBManager database;
	String object;
	String SYSTEMUSER="systemuser";
	String TYPE="type";
	GetIdName gid;
	String EVENT="event"; 
	
	
	public void getData(DBManager database, String object)
	{
		this.database = database;
		this.object =object;
		gid = new GetIdName(database);
		TYPE = gid.getId(TYPE);
		EVENT = gid.getId(EVENT);
	}

	public void getResult() 
	{
		RecordSearch rs =new RecordSearch(database);
		Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
		Condition.put("mid", object);
		Condition.put("pid", EVENT);
		rs.setConditionMap(Condition);
		String result[] = rs.getArrayFromResultSet();
		Event startEvent;
		for(int i=0;i<result.length;i++)
		{
			String split[] =result[i].split("\t");
			try 
			{
				Property property = new Property(database);
				property.setMid(split[Column.pv_index-1]);
				property.setPid(TYPE);
				String classname = property.getValue();
				Class C = Class.forName("event."+CapitalChar.makeFirstCharCapital(classname));
				startEvent = (Event)C.newInstance();
				startEvent.SetData("pool", "","",SYSTEMUSER,"add");
				startEvent.initializationData(database);
				startEvent.setObject(split[Column.pv_index-1]);
				String Methodname = "after"+CapitalChar.makeFirstCharCapital("add");
				Method method = C.getDeclaredMethod(Methodname,new Class[0]);
				method.invoke(startEvent, new Object[0]);
			} 
			catch (ClassNotFoundException e) 
			{
				e.printStackTrace();
			}
			catch (InstantiationException e) 
			{
				e.printStackTrace();
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}	
	}

}
